package com.example.view.data;

import android.content.Context;
import android.util.JsonReader;

import com.example.view.bean.CityBean;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;


public class CityDao {

    public void init(Context context) {
        if (isEmpty()) {
            try {
                InputStream is = context.getAssets().open("citys.json");
                JsonReader reader = new JsonReader(new InputStreamReader(is));
                this.parseProvince(reader);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void parseProvince(JsonReader reader) {
        try {
            reader.beginArray();
            for (int p = 1; reader.hasNext(); p++) {
                reader.beginObject();
                while (reader.hasNext()) {
                    String name = reader.nextName();
                    if ("name".equals(name)) {
//                        L.e("parseProvince if");
                        this.insert(new CityBean(p, reader.nextString(), null));
                    } else if ("city".equals(name)) {
                        this.parseCity(reader, p);
                    }
                }
                reader.endObject();
            }
            reader.endArray();
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void parseCity(JsonReader reader, int pid) {
        try {
            reader.beginArray();
            for (int c = 1; reader.hasNext(); c++) {
                reader.beginObject();
                while (reader.hasNext()) {
                    String name = reader.nextName();
                    if ("name".equals(name)) {
                        this.insert(new CityBean(pid * 100 + c, reader.nextString(), pid));
                    } else if ("area".equals(name)) {
                        parseDistrict(reader, pid * 100 + c);
                    }
                }
                reader.endObject();
            }
            reader.endArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void parseDistrict(JsonReader reader, int i) {
        try {
            reader.beginArray();
            for (int d = 1; reader.hasNext(); d++) {
                this.insert(new CityBean(i * 100 + d, reader.nextString(), i));
            }
            reader.endArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void insert(CityBean cityBean) {
//        Realm realm = Realm.getDefaultInstance();
//        realm.beginTransaction();
//        realm.insert(cityBean);
//        realm.commitTransaction();
    }


    /**
     * 根据 区域id 获取 地区
     *
     * @param pid 上一级 id
     * @return
     */
    public List<CityBean> getRegion(Integer pid) {
//        Realm realm = Realm.getDefaultInstance();
//        RealmResults<CityBean> beans = realm.where(CityBean.class).equalTo("pid", pid).findAll();
//        return realm.copyFromRealm(beans);
        return null;
    }


    public boolean isEmpty() {
//        Realm realm = Realm.getDefaultInstance();
//        return realm.where(CityBean.class).findFirst() == null;
        return true;
    }

}
